import pymysql
from prettytable import from_db_cursor


#  查询
def Select():
    """TODO: Docstring for Select.
    :returns: TODO

    """
    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    SQL = "select * from account"
    try:
        cursor.execute(SQL)
        tb = from_db_cursor(cursor)
        print(tb)
    except:
        print("Error: unable to fetch data")
    db.close()


# 按照类型查询
def Select_Type():
    """TODO: Docstring for Select_Type.
    :returns: TODO

    """
    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    type = input("输入type: > ")
    SQL = "select * from account where type = '" + type + "'"
    try:
        cursor.execute(SQL)
        tb = from_db_cursor(cursor)
        print(tb)
    except:
        print("Error: unable to fetch data")
    db.close()



# 插入
def Insert():
    """TODO: Docstring for Insert.
    :returns: TODO

    """
    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    Type = input("输入Type: > ")
    Username = input("输入Username: >")
    Account = input("输入Account: >")
    Passworld = input("输入Passworld: >")
    Url = input("输入Url: >")
    SQL = "insert into account (Type, Username, Account, Passworld, Url) values ('%s', '%s', '%s', '%s', '%s')" % (Type, Username, Account, Passworld, Url)
    try:
        row = cursor.execute(SQL)
        if row != 0:
            print("添加成功")
        else:
            print("添加失败")
        db.commit()
    except:
        db.rollback()
    db.close()


# 修改_ID
def Update_Id():
    """TODO: Docstring for Update.

    :returns: TODO

    """
    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    ID = input("ID: > ")
    Passworld = input("Passworld: > ")
    SQL = "Update account SET Passworld= '" + Passworld + "'Where id = '" + ID + "'"
    try:
        row = cursor.execute(SQL)
        if row != 0:
            print("修改成功")
        else:
            print("修改失败")
        db.commit()
    except:
        db.rollback()
    db.close()


# 修改_Account
def Update_Account():
    """TODO: Docstring for Update_Account.

    :returns: TODO

    """

    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    Account = input("Account: > ")
    Passworld = input("Passworld: > ")
    SQL = "Update account SET Passworld= '" + Passworld + "'Where Account = '" + Account + "'"
    try:
        row = cursor.execute(SQL)
        if row != 0:
            print("修改成功")
        else:
            print("修改失败")
        db.commit()
    except:
        db.rollback()
    db.close()


# 修改_Type
def Update_Type():
    """TODO: Docstring for Update_Type.

    :returns: TODO

    """

    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    type = input("type: > ")
    Passworld = input("Passworld: > ")
    SQL = "Update account SET Passworld= '" + Passworld + "'Where type = '" + type + "'"
    try:
        row = cursor.execute(SQL)
        if row != 0:
            print("修改成功")
        else:
            print("修改失败")
        db.commit()
    except:
        db.rollback()
    db.close()


 # 删除
def Delete():
    """TODO: Docstring for Delete.

    :returns: TODO

    """

    db = pymysql.connect("localhost", "root", "123456", "account")
    cursor = db.cursor()
    ID = input("ID: > ")
    SQL = "Delete from account where ID = " + ID
    try:
        row = cursor.execute(SQL)
        if row != 0:
            print("删除成功")
        else:
            print("删除失败")
        db.commit()
    except:
        db.rollback()
    db.close()